package com.kfm.controller;

import com.kfm.commons.AjaxResult;
import com.kfm.config.FileConfig;
import com.kfm.entity.User;
import com.kfm.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;

@RestController
@RequestMapping("/user")
@CrossOrigin("*")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user) {
        // 用户登录成功，如果没有用户的文件夹，就创建一个
        User loginUser = userService.selectByUser(user);
        if (loginUser != null) {
            String path = FileConfig.getLocalPath() + File.separator + loginUser.getUsername() + File.separator + "我的资源";
            File file = new File(path);
            if (file.isDirectory() || !file.exists()) {
                boolean mkdirs = file.mkdirs();
            }
            return AjaxResult.success(loginUser);
        }
        return AjaxResult.fail();
    }
}
